package geos

import (
	"gitee.com/lailonghui/vehicle-supervision-framework/pkg/asserts"
	"github.com/paulmach/orb"
	"github.com/paulmach/orb/encoding/wkt"
	"strings"
)

func ResolvePoint(str string) *Point {
	str = strings.ReplaceAll(str, "SRID=4326;", "")
	point, err := wkt.UnmarshalPoint(str)
	asserts.Nil(err, err)
	return orbPointResolve(&point)
}

func ResolveLineString(str string) []*Point {
	str = strings.ReplaceAll(str, "SRID=4326;", "")
	lineString, err := wkt.UnmarshalLineString(str)
	asserts.Nil(err, err)
	ps := make([]*Point, 0)
	for _, p := range lineString {
		ps = append(ps, orbPointResolve(&p))
	}
	return ps
}

func orbPointResolve(point *orb.Point) *Point {
	if point == nil {
		return nil
	}
	return &Point{
		Longitude: point[0],
		Latitude:  point[1],
	}
}
